local M = {}

M.cmd = vim.cmd

M.autocmd = function(event, opts)
  vim.api.nvim_create_autocmd(event, opts or {})
end

M.augroup = function(name)
  return vim.api.nvim_create_augroup(name, { clear = true })
end

-- mode: n,i,x(visual only),c(command-line),t(terminal)
-- multi-mode: {"n","x"}
---@param mode string or table
---@param l string
---@param r string or function
---@param opts? table
M.map = function(mode, l, r, opts)
  local options = { silent=true }
  if opts then
    options = vim.tbl_extend('force', options, opts)
  end
  vim.keymap.set(mode, l, r,  options)
end

--Function to set a local buffer key mapping
M.local_map = function(mode, lhs, rhs, opts)
    local bufnr = vim.api.nvim_get_current_buf()
    local options = { noremap = true, silent = true }
    if opts then
        options = vim.tbl_extend('force', options, opts)
    end
    if type(rhs) == "function" then
        vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, noremap = options.noremap, silent = options.silent })
    else
        vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, options)
    end
end

return M
-- EOP
